<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>View Active Objects</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<!-- YAML css -->
<link href="css/sl-www-integration.css" rel="stylesheet" type="text/css"/>
<!--[if lte IE 7]>
<link href="../css/patches/patch_my_layout.css" rel="stylesheet" type="text/css" />
<![endif]-->
<!-- (en) Add your meta data here -->
<!-- Add special class to display Meta font -->
<script type="text/javascript" src="js/typeface.js"></script>
<script type="text/javascript" src="js/metabooklf-roman_regular.typeface.js"></script>
</head>
<body>
<div class="page_margins">
	<div class="page">
		<div id="header">
			<div id="SLWWWIntegrationLogo">
				<a href="/">
					<img alt="SL WWW Integration Logo" src="images/SLWWWIntegration.gif"/>
				</a>
			</div>
			<h1>View Active Objects</h1>
			<span>Internal SQLite database</span>
		</div>
		<!-- begin: main content area #main -->
		<div id="main">
<?php
include_once("common.inc.php");

/**
* @param $URL string	Remote URL to call
* @param $postFields	Parameters to call, using &= notation for forms
* @return Array containing 'HTTP_CODE' and 'HTTP_DATA'
* @access public
*/
function cURL($URL, $postFields, $timeOut=0)
{		
	// prepare cURL call
	$ch = curl_init();

	curl_setopt($ch, CURLOPT_URL, $URL);
	curl_setopt($ch, CURLOPT_POST, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);	// SL has self-signed certs
	curl_setopt($ch, CURLOPT_POSTFIELDS, $postFields);
	if ($timeOut != 0)
		curl_setopt($ch, CURLOPT_TIMEOUT, $postFields);

	$dataResult = curl_exec($ch);
	$curlResult = curl_getinfo($ch, CURLINFO_HTTP_CODE);
	curl_close($ch);
	return(array('HTTP_CODE' => $curlResult, 'HTTP_DATA' => $dataResult));
}



// if $_POST[] is empty write form

if ($_POST)
{
?>
			<div id="col3">
			<!--<div id="col3_content" class="clearfix"> <a id="content" name="content"></a>-->
					<!-- skiplink anchor: Content -->

<?php
	$objectKeyArray = array();
	if(isset($_POST['objectKey'])){
		$objectKeyArray = $_POST['objectKey'];
		//echo $skillsArray; // Echos the "Array" object
		$i = 0;
		foreach ($objectKeyArray as $key => $objectUUID)
		{ 
        	$i++;
			//echo "objectKey $i || Array Key = $key || Value = $objectUUID<br />";
			
			$red	= floatval($_POST["red"][$objectUUID]);
			$green	= floatval($_POST["green"][$objectUUID]);
			$blue	= floatval($_POST["blue"][$objectUUID]);
			
				
			// Get PermURL for this item from database
			
			$result = $db->query("SELECT PermURL FROM objects WHERE Key = '$objectUUID';");
			$thisPermURL = $result->fetchSingle(); // should be unique
			
			if (isset($_POST["update"][$objectUUID]))
			{
				if ($thisPermURL)
				{
					echo "Sending RGB values <$red,$green,$blue> to object $objectUUID...<br />";

					$res = cURL($thisPermURL, "red=$red&green=$green&blue=$blue", 60);
						
					if ($res['HTTP_CODE'] != 200)
						echo "Error " . $res['HTTP_CODE'] . " contacting object $objectUUID"
							. $res['HTTP_DATA'];
					else
						echo "$objectUUID sent '" . $res['HTTP_DATA'] . "'";
					echo "<br />";
				
				} // 
				else
					echo "Empty PermURL for object $objectUUID<br />";
			}
			else "No commands for object $objectUUID<br />";
				
			if (isset($_POST["delete"][$objectUUID]))
			{
				echo "Deleting object $objectUUID<br />";
				if ($thisPermURL)
				{
					$resDel = cURL($thisPermURL, "delete=true", 60);
					
					if ($resDel['HTTP_CODE'] != 200)
					{
						echo "Error " . $resDel['HTTP_CODE'] . " deleting object $objectUUID"
						. $resDel['HTTP_DATA'];
					
						$resultDel = $db->query("DELETE FROM objects WHERE Key = '$objectUUID';");	
					}
					else
						echo "$objectUUID (probably) deleted. Result: '" . $resDel['HTTP_DATA'] . "'";
					echo "<br />";
			
				}
			}
			/*else
				echo "Keeping object $objectUUID<br />";	*/	
		}
		echo "<hr />";
	} //foreach
/*
	 echo "<hr />Full dump";
	 echo "<br />";
	
	 var_dump($_REQUEST);
*/	
	echo "<hr /><a href='" . substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1) . "'>New query</a><br />";
}
else
{
?>
			<div id="col1">
			<!--<div id="col3_content" class="clearfix"> <a id="content" name="content"></a>-->
					<!-- skiplink anchor: Content -->

	<form name="sender" method="post">
	<table border="1">
	<thead>
		<tr>
			<td>Id</td>
			<td>Key</td>
			<td>Name</td>
			<td>PermURL</td>
			<td>Location</td>
			<td>OwnerKey</td>
			<td>OwnerName</td>
			<td>LastUpdate</td>
			<td>|</td>
			<td>Red</td>
			<td>Green</td>
			<td>Blue</td>
			<td>Update?</td>
			<td>Delete?</td>
		</tr>
	</thead>
	<tbody>
<?php
// fetch rows from the 'objects' database table

	$result = $db->query("SELECT * FROM objects");

	$rows = $result->fetchAll();

	foreach($rows as $row)
	{
		echo "\t\t<tr>\n\t\t\t<td>".$row['id'].'</td>'.
		 '<td>'.$row['Key'].'</td>'.
		 '<td>'.$row['Name'].'</td>'.
		 '<td>'.$row['PermURL'].'</td>'.
		 '<td>'.$row['Location'].'</td>'.
		 '<td>'.$row['OwnerKey'].'</td>'.
		 '<td>'.$row['OwnerName'].'</td>'.
		 '<td>'.$row['LastUpdate'].'</td>';
?>
			<td>|</td>
			<td><input type="text" size="3" name="red[<?php echo $row['Key']; ?>]" value="0"></td>
			<td><input type="text" size="3" name="green[<?php echo $row['Key']; ?>]" value="0"></td>
			<td><input type="text" size="3" name="blue[<?php echo $row['Key']; ?>]" value="0"></td>
			<td><input type="checkbox" name="update[<?php echo $row['Key']; ?>]" value="0"></td>
			<td><input type="checkbox" name="delete[<?php echo $row['Key']; ?>]" value="0"></td>
			<input type="hidden" name="objectKey[]" value="<?php echo $row['Key']; ?>">
		</tr>
<?php
	} // end foreach
?>
	</tbody>
	</table>
	<button type="submit" value="Submit">Submit</button>
	</form>
<?php
} // end create form
?>
					<!--</div>-->
				<div id="ie_clearing">&nbsp;</div>
				<!-- End: IE Column Clearing -->
			</div>
			<!-- end: #col3 -->
		</div>
		<!-- end: #main -->
		<!-- begin: #footer -->
		<div id="footer">© 2012 by Beta Technologies. Some rights reserved.<br />
Layout based on <a href="http://www.yaml.de/">YAML</a></div>
		<!-- end: #footer -->
	</div>
</div>
</body>
</html>
